package com.github.iot.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatEditText;

import com.github.iot.R;
import com.github.iot.dao.CommonDaoUtils;
import com.github.iot.dao.DaoUtilsStore;
import com.github.iot.entity.Button;
import com.github.iot.entity.Door;
import com.github.iot.entity.Light;
import com.github.iot.entity.Showcase;
import com.github.iot.entity.Showroom;
import com.github.iot.utils.BarUtils;
import com.github.iot.utils.FastJsonUtils;
import com.github.iot.utils.KeyboardUtils;
import com.github.iot.utils.ToastUtils;
import com.github.iot.view.RxTitle;
import com.github.iot.view.dialogs.fragment.SimpleDialogFragment;
import com.github.iot.view.dialogs.iface.ISimpleDialogListener;

import java.util.List;
import java.util.UUID;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 添加展柜Activity
 *
 * @Author: 梁声洪
 * @Date: 2019/12/06 9:14
 * @Version: v1.0.0
 */
public class AddShowcaseActivity extends AppCompatActivity implements ISimpleDialogListener {

    private static final int REQUEST_SIMPLE_DIALOG = 2;

    @BindView(R.id.rx_title)
    RxTitle mTitle;

    @BindView(R.id.et_name)
    AppCompatEditText mName;

    @BindView(R.id.et_address)
    AppCompatEditText mAddress;

    private static Showroom showroom;
    private static Integer sort;

    private static CommonDaoUtils<Button> buttonDaoUtils = DaoUtilsStore.getInstance().getButtonDaoUtils();
    private static CommonDaoUtils<Door> doorDaoUtils = DaoUtilsStore.getInstance().getDoorDaoUtils();
    private static CommonDaoUtils<Light> lightDaoUtils = DaoUtilsStore.getInstance().getLightDaoUtils();
    private static CommonDaoUtils<Showroom> showroomDaoUtils = DaoUtilsStore.getInstance().getShowroomDaoUtils();
    private static CommonDaoUtils<Showcase> showcaseDaoUtils = DaoUtilsStore.getInstance().getShowcaseDaoUtils();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BarUtils.setStatusBarColor(this, R.color.colorPrimaryDark);
        setContentView(R.layout.activity_add_showcase);

        ButterKnife.bind(this);
        initView();
        initData();
    }

    private void initView() {
        showroom = FastJsonUtils.strToBean(getIntent().getStringExtra("showroom"), Showroom.class);
        sort = getIntent().getIntExtra("showcaseSize", 0);
        mTitle.setLeftFinish(this);
    }

    private void initData() {
        List<Showcase> list = showcaseDaoUtils.queryAll();
        mAddress.setText(String.valueOf(list.size() + 1));
    }

    @OnClick({R.id.btn_add_showcase})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_add_showcase:
                KeyboardUtils.hideSoftInput(AddShowcaseActivity.this);
                SimpleDialogFragment.createBuilder(AddShowcaseActivity.this, getSupportFragmentManager())
                        .setTitle("系统提示")
                        .setMessage("您确定要添加展柜信息吗？")
                        .setPositiveButtonText("确定")
                        .setNegativeButtonText("取消")
                        .setCancelable(false)
                        .setRequestCode(REQUEST_SIMPLE_DIALOG)
                        .show();
                break;
            default:

                break;
        }
    }

    /**
     * 添加展柜
     */
    private void addShowcase() {
        String name = mName.getText().toString();
        String address = mAddress.getText().toString();

        if (TextUtils.isEmpty(name)) {
            ToastUtils.warning("展柜名称不能为空！");
            return;
        }

        if (TextUtils.isEmpty(address)) {
            ToastUtils.warning("地址不能为空！");
            return;
        }

        if (Integer.valueOf(address) < 1) {
            ToastUtils.warning("地址范围为1到255");
            return;
        }

        if (Integer.valueOf(address) >= 256) {
            ToastUtils.warning("地址范围为1到255");
            return;
        }

        String sql = "WHERE ADDRESS = ?";
        List<Showcase> list = showcaseDaoUtils.queryByNativeSql(sql, new String[]{address});
        if (list.size() > 0) {
            ToastUtils.warning("地址已存在！");
            return;
        }

        Showcase showcase = new Showcase();
        showcase.setShowroomId(showroom.getId());
        showcase.setName(name);
        showcase.setAddress(Integer.valueOf(address));
        showcase.setSort(sort);

        long i = showcaseDaoUtils.insert(showcase);
        if (i > 0) {
            try {
                String uuid = UUID.randomUUID().toString();
                for (int i1 = 1; i1 <= 2; i1++) {
                    Door door = new Door();
                    door.setShowcaseId(i);
                    door.setName("柜门" + i1);
                    door.setTypeName("柜门");
                    door.setType("柜门");
                    door.setUuid(uuid);
                    door.setAddress(Integer.valueOf(address));

                    if (1 == i1) {
                        door.setDownClose(203);
                        door.setUpClose(202);
                        door.setDownOpen(201);
                        door.setUpOpen(200);
                    }

                    if (2 == i1) {
                        door.setDownClose(207);
                        door.setUpClose(206);
                        door.setDownOpen(205);
                        door.setUpOpen(204);
                    }

                    door.setStatus(1);
                    door.setSort(i1);
                    doorDaoUtils.insert(door);
                }

                for (int i1 = 1; i1 <= 2; i1++) {
                    Light light = new Light();
                    light.setShowcaseId(i);
                    light.setName(i1 + "号灯");
                    light.setType(i1);
                    light.setProgress(0);
                    light.setStatus(1);
                    light.setSort(200);
                    lightDaoUtils.insert(light);
                }

                for (int i1 = 1; i1 <= 4; i1++) {
                    Button button = new Button();
                    button.setShowcaseId(i);
                    button.setName("按钮" + i1);
                    button.setStatus(-1);
                    button.setSort(300);

                    if (i1 == 1) {
                        button.setOpen(208);
                        button.setClose(209);
                    }

                    if (i1 == 2) {
                        button.setOpen(210);
                        button.setClose(211);
                    }

                    if (i1 == 3) {
                        button.setOpen(212);
                        button.setClose(213);
                    }

                    if (i1 == 4) {
                        button.setOpen(214);
                        button.setClose(215);
                    }

                    buttonDaoUtils.insert(button);
                }

                showroom.setNum(showroom.getNum() + 1);
                showroomDaoUtils.update(showroom);

                ToastUtils.success("展柜信息添加成功");
                finish();
            } catch (Exception e) {
                e.printStackTrace();
                showcase.setId(i);
                showcaseDaoUtils.delete(showcase);
                ToastUtils.error("展柜信息添加失败");
            }
        } else {
            ToastUtils.error("展柜信息添加失败");
        }
    }

    @Override
    public void onNegativeButtonClicked(int requestCode) {

    }

    @Override
    public void onNeutralButtonClicked(int requestCode) {

    }

    @Override
    public void onPositiveButtonClicked(int requestCode) {
        if (requestCode == REQUEST_SIMPLE_DIALOG) {
            addShowcase();
        }
    }

}